package com.musifind.activities;

import java.text.ParseException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.fizy.client.controllers.FizyControllers;
import com.fizy.client.objects.Album;
import com.fizy.client.objects.Track2;
import com.musifind.adapters.SearchAdapterTracks;
import com.musifind.common.DateTimeUtils;
import com.musifind.common.GeneralHelper;
import com.musifind.models.SearchItems;

public class TracksOfAlbumActivity extends Activity {

	private SearchAdapterTracks searchAdapterTracks;
	private ArrayList<SearchItems> searchArrayTracks;
	private ListView lstSearchTracks;
	private ImageView imgLogo;
	private ProgressDialog progressDialog = null;
	private int lvGlobalPosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tracks_of_album);

		imgLogo = (ImageView) findViewById(R.id.imgBlankPage);

		searchArrayTracks = new ArrayList<SearchItems>();
		searchAdapterTracks = new SearchAdapterTracks(TracksOfAlbumActivity.this,
				R.layout.search_list_items_row_tracks, searchArrayTracks);
		lstSearchTracks = (ListView) findViewById(R.id.lstSearchTrackItems);
		lstSearchTracks.setAdapter(searchAdapterTracks);

		lstSearchTracks.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				lvGlobalPosition = position;
				new OpenTrackAsyncTask().execute();

			}
		});

		bundleDatas();
	}

	public void getTrackResults(final String albumId) {
		if (albumId != null && !albumId.isEmpty()) {
			runOnUiThread(new Runnable() {
				public void run() {
					FizyControllers controllers = new FizyControllers();
					Album album2 = controllers.getAlbumTracks(albumId);
					for (Track2 track2 : album2.getTracks()) {
						String strAlbumCover = album2.getCover_medium();
						String strTrackName = track2.getName();
						String strArtistName = track2.getArtist().getName();
						String strAlbumName = album2.getName();
						String strAlbumYear = album2.getRelease_date();
						String strAlbumYearNew = null;
						try {
							strAlbumYearNew = DateTimeUtils.stringToDate(strAlbumYear);
						} catch (ParseException e) {
							Log.e("Date Conversion Problem", "Date hasn't parsed");
						}
						searchArrayTracks.add(new SearchItems(track2.getId(), strAlbumCover, strTrackName,
								strArtistName, strAlbumName, " (" + strAlbumYearNew + ")"));
					}

					if (searchAdapterTracks != null) {
						searchAdapterTracks.notifyDataSetChanged();
					}
					if (searchArrayTracks != null && searchArrayTracks.size() > 0) {
						imgLogo.setVisibility(View.INVISIBLE);
					}
				}
			});
		}
	}

	public void bundleDatas() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			String albumId = String.valueOf(bundle.getInt("albumId"));
			String albumName = bundle.getString("albumName");
			setTitle(albumName + " şarkıları");
			getTrackResults(albumId);
		}
	}

	public void openMusicPlayer() {
		SearchItems item = (SearchItems) searchAdapterTracks.getItem(lvGlobalPosition);
		FizyControllers controllers = new FizyControllers();
		Track2 track2 = controllers.getTrack(String.valueOf(item.getTrackId()));
		if (track2 != null) {
			Intent i = new Intent(TracksOfAlbumActivity.this, MusicPlayerActivity.class);
			i.putExtra("track_id", String.valueOf(track2.getId()));
			i.putExtra("track_url", track2.getSecure_url());
			i.putExtra("track_name", track2.getName());
			i.putExtra("artist_name", track2.getArtist().getName());
			i.putExtra("album_name", track2.getAlbum().getName());
			i.putExtra("album_year", track2.getAlbum().getRelease_date());
			i.putExtra("cover_image", track2.getAlbum().getCover_large());
			startActivity(i);
		} else {
			GeneralHelper.showRedToast(getApplicationContext(), "URL bulunamadı.");
		}
	}

	public class OpenTrackAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(TracksOfAlbumActivity.this);
			progressDialog.setTitle("Çalmaya hazırlanıyor");
			progressDialog.setMessage("Lütfen bekleyiniz...");
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			openMusicPlayer();
			return null;
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}
	}
}
